package com.lagou.edu.strategy;

import rx.internal.util.LinkedArrayList;

import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

public class DefaultRejectStrategy {
    private List<Long> record = new LinkedList<>();

    public synchronized void add(int second) {
        Long currentTime = System.currentTimeMillis();
        // 放入新的
        record.add(currentTime);

        // 删除老的
        Long expireTime = System.currentTimeMillis() - 1000 * second;
        int index = 0;
        for (int i = 0; i < record.size(); i++) {
            if (record.get(i) > expireTime) {
                break;
            }
            index = i;
        }
        record = record.subList(index, record.size());
    }

    public synchronized boolean checkIsAttack(int times) {
        return record.size() > times;
    }

    public synchronized int getCount() {
        return record.size();
    }
}
